package com.hdoit.kylin.logical.document.dao.impl;
import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.kylin.logical.document.dao.IAchievementDao;
import com.hdoit.kylin.logical.document.model.Achievement;
import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.viewmodel.document.AchievementSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
* @author luozhicheng
* @email
* @createtime 2017年07月18日 20:43:06
* @comment
*/
@Component
public class AchievementDaoImpl extends SuperDao<Achievement> implements IAchievementDao{
	
	@Override
	public int queryCountByGroupId(AchievementSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		if(search.getGroupId()!=null){
			finderExpression.select("count(DISTINCT achievement.id) as count")
										 .from(Achievement.class,"achievement")
										 .leftJoin(Group.class,"group_info")
										 .on("achievement.groupId","group_info.groupId")
										 .andWhere("achievement.groupId=#{groupId}")
										 .setParam("groupId", search.getGroupId())
										 .setMainTableAlias("achievement");
		}
		else{
			finderExpression.select("count(DISTINCT achievement.id) as count")
										 .from(Achievement.class,"achievement")
										 .leftJoin(Group.class,"group_info")
										 .on("achievement.groupId","group_info.groupId")
										 .andWhere("group_info.groupId IS NOT NULL")
										 .setMainTableAlias("achievement");		
		}
		return queryCount(finderExpression,search);	
	}

	@Override
	public List<Achievement> queryListByGroupId(AchievementSearch search,PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		if(search.getGroupId()!=null){
			finderExpression.select("achievement.*,group_info.groupName as theGroupName")
										 .from(Achievement.class,"achievement")
										 .leftJoin(Group.class,"group_info")
										 .on("achievement.groupId","group_info.groupId")
										 .andWhere("achievement.groupId=#{groupId}")
										 .setParam("groupId", search.getGroupId())
										 .setMainTableAlias("achievement");		
		}
		else{
			finderExpression.select("achievement.*,group_info.groupName as theGroupName")
										 .from(Achievement.class,"achievement")
										 .leftJoin(Group.class,"group_info")
										 .on("achievement.groupId","group_info.groupId")
										 .andWhere("group_info.groupId IS NOT NULL")
										 .setMainTableAlias("achievement");		
		}
	

		return queryList(finderExpression,search,pagerModel);
	}

	
}
